package biz.datalk.industrialland.admin.modules.system.controller;

import biz.datalk.industrialland.admin.modules.system.pojo.dto.SysRoleDTO;
import biz.datalk.industrialland.admin.modules.system.service.ISysRoleService;
import biz.datalk.industrialland.common.encrypt.annotation.RSAES;
import biz.datalk.industrialland.common.json.annotation.Json;
import biz.datalk.industrialland.common.log.aspect.annotation.SysLog;
import biz.datalk.industrialland.common.log.def.OptEnum;
import biz.datalk.industrialland.common.result.UnifyResult;
import biz.datalk.industrialland.common.validated.BatchDelete;
import biz.datalk.industrialland.common.validated.Insert;
import biz.datalk.industrialland.common.validated.Update;
import biz.datalk.industrialland.mbg.pojo.po.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tarofang@163.com
 * @date 2019-08-18
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    private final ISysRoleService sysRoleService;

    @Autowired
    public SysRoleController(ISysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }


    @RSAES
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = SysRole.class, filter = "createBy,updateBy")
    @SysLog(OptEnum.LAND_ROLE_10301)
    public UnifyResult list(@RequestBody SysRoleDTO sysRoleDTO) {
        return sysRoleService.list(sysRoleDTO);
    }

    @RSAES
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_ROLE_10302)
    public UnifyResult addRole(@RequestBody @Validated({Insert.class}) SysRoleDTO sysRoleDTO) {
        return sysRoleService.addRole(sysRoleDTO);
    }

    @RSAES
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_ROLE_10303)
    public UnifyResult updateRole(@RequestBody @Validated({Update.class}) SysRoleDTO sysRoleDTO) {
        return sysRoleService.updateRole(sysRoleDTO);
    }

    @RSAES
    @PostMapping(value = "/batchDelete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_ROLE_10304)
    public UnifyResult batchDelete(@RequestBody @Validated({BatchDelete.class}) SysRoleDTO sysRoleDTO) {
        return sysRoleService.batchDelete(sysRoleDTO);
    }

    @RSAES
    @PostMapping(value = "/allRoles", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = SysRole.class, include = "id,roleName,roleCode,description,sysOrgCode")
    @SysLog(OptEnum.LAND_ROLE_10305)
    public UnifyResult allRoles() {
        List<SysRole> allRoles = sysRoleService.allValidRoles();
        return UnifyResult.success().setData(allRoles);
    }
}
